package com.comp437.uno.view.playroomlist;


import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.comp437.uno.model.Model;
import com.comp437.uno.view.playroom.PlayRoomView;
import com.example.comp437uno.R;

public class ItemAdapter extends ArrayAdapter<String>{
	private final Context context;
    private final String[] Ids;
    private final int rowResourceId;
    private final Model model;
    public ItemAdapter(Context context, int textViewResourceId, String[] objects) {

        super(context, textViewResourceId, objects);
        this.model = Model.getModel();
        this.context = context;
        this.Ids = objects;
        this.rowResourceId = textViewResourceId;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(rowResourceId, parent, false);
        ImageButton imageView = (ImageButton) rowView.findViewById(R.id.imagebutton_playroomList_item_join);
        TextView textView = (TextView) rowView.findViewById(R.id.textview_playroomList_item_textView);

        int id = Integer.parseInt(Ids[position]);
        textView.setText(model.GetbyId(id).Name);
        if(model.GetbyId(id).Num==3){
        	
        	imageView.setOnClickListener(new bt_noJoinOnClickListener());
        	imageView.setBackgroundResource(R.drawable.nojoin);
        	
        }
        else
        	imageView.setOnClickListener(new bt_joinOnClickListener(id));
        // get input stream
        return rowView;

    }
    
    class bt_noJoinOnClickListener implements android.view.View.OnClickListener{

    	
		@Override
		public void onClick(View v) {
				Model.getModel().showToast(context, "The Room is Full.");
		}
    	
    }
    
    class bt_joinOnClickListener implements android.view.View.OnClickListener{

    	int id;
    	public bt_joinOnClickListener(int id){
    		this.id = id;
    	}
   
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(model.addRoom(id)){
				Intent intent=new Intent();
				intent.setClass(context, PlayRoomView.class);
				intent.putExtra("roomID", model.GetbyId(id).Name);
				context.startActivity(intent);
			}
			else
				Model.getModel().showToast(context, "Failed");

		}
    	
    }
 
	
}
